package com.example.enginsdk;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.csizg.imemodule.entity.HwrRecogResult;
import com.csizg.imemodule.entity.HwrRecogResultItem;
import com.csizg.imemodule.eventbus.OnHandWritingPoints;
import com.csizg.imemodule.eventbus.onHandWritingReleaseEvent;
import com.csizg.imemodule.service.PinyinIME;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class App extends Application {

    private static App sInstance;

    //获取全局Context
    public static Context getAppContext() {
        return sInstance.getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        // 注册eventBus
        if (!LauncherModel.getInstance().getGlobalEventBus().isRegistered(this)) {
            LauncherModel.getInstance().getGlobalEventBus().register(this);
        }
        ImeSdkManager.getSKDManager().initData(this);
    }

    /**
     * 手写
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(OnHandWritingPoints event) {
        List<Short> mSBPoint = event.getPointsList();
        HwrRecogResult recogResult = new HwrRecogResult();
        boolean recognitionResult = HdwManager.getInstance().recognitionData(mSBPoint, recogResult);
        mSBPoint.clear();
        if (recognitionResult) {
            if (recogResult != null) {
                ArrayList<HwrRecogResultItem> recogItemList = recogResult.getResultItemList();
                if (recogItemList != null) {
                    ArrayList<String> words = new ArrayList<>();
                    for (int index = 0; index < recogItemList.size(); index++) {
                        String strTmp = recogItemList.get(index).getResult();
                        words.add(strTmp);
                    }
                    PinyinIME.getPinyinIME().handleHandwritingResult(words, false);
                }
            }
        }
    }

    /**
     * 释放手写
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(onHandWritingReleaseEvent event) {
        HdwManager.getInstance().hciHwrRelease();
    }
}
